package org.jeecg.modules.monitoring.j_monitoring.controller;

import java.util.Arrays;
import java.util.List;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.monitoring.j_monitoring.utils.EquipmentUtils;
import org.jeecg.modules.monitoring.j_monitoring.entity.MGlUps;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MGlUpsMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMGlUpsService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: UPS电源
 * @Author: xiaoyq
 * @Date:   2023-02-22
 * @Version: V1.0
 */
@Api(tags="UPS电源")
@RestController
@RequestMapping("/j_monitoring/mGlUps")
@Slf4j
public class MGlUpsController extends JeecgController<MGlUps, IMGlUpsService> {
	@Autowired
	private IMGlUpsService mGlUpsService;
	@Resource
	private MGlUpsMapper mGlUpsMapper1;

	/**
	 * 分页列表查询
	 *
	 * @param mGlUps
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "UPS电源-分页列表查询")
	@RequiresPermissions("UPS电源:查询")
	@ApiOperation(value="UPS电源-分页列表查询", notes="UPS电源-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(MGlUps mGlUps,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Result<Page<MGlUps>> result=new Result<>();
		Page<MGlUps> pagelist = new Page(pageNo, pageSize);
		QueryWrapper<MGlUps> queryWrapper = QueryGenerator.initQueryWrapper(mGlUps, req.getParameterMap());
		pagelist = mGlUpsMapper1.getPageList(pagelist,queryWrapper);

		for (MGlUps ups : pagelist.getRecords()){
			ups.setMonitorStatus(EquipmentUtils.getRingStatus(ups.getWhetherToMonitor()));
		}

		result.setCode(200);
		result.setSuccess(true);
		result.setResult(pagelist);
		return result;
	}

	/**
	 *   添加
	 *
	 * @param mGlUps
	 * @return
	 */
	@RequiresPermissions("UPS电源:添加")
	@AutoLog(value = "UPS电源-添加")
	@ApiOperation(value="UPS电源-添加", notes="UPS电源-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody MGlUps mGlUps) {
		mGlUpsService.save(mGlUps);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param mGlUps
	 * @return
	 */
	@AutoLog(value = "UPS电源-编辑")
	@ApiOperation(value="UPS电源-编辑", notes="UPS电源-编辑")
	@RequiresPermissions("UPS电源:编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody MGlUps mGlUps) {
		mGlUpsService.updateById(mGlUps);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "UPS电源-通过id删除")
	@ApiOperation(value="UPS电源-通过id删除", notes="UPS电源-通过id删除")
	@RequiresPermissions("UPS电源:删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		mGlUpsService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "UPS电源-批量删除")
	@ApiOperation(value="UPS电源-批量删除", notes="UPS电源-批量删除")
	@RequiresPermissions("UPS电源:批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.mGlUpsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	 /**
	  *  监控
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "UPS电源-添加监控")
	 @ApiOperation(value="UPS电源-添加监控", notes="UPS电源-添加监控")
	 @RequiresPermissions("UPS电源:监控")
	 @RequestMapping(value = "/monitor", method = {RequestMethod.POST})
	 @Transactional(rollbackFor = Exception.class)
	 public Result<String> monitor(@RequestParam(name="ids",required=true) String ids) {
		 List<String> idList = Arrays.asList(ids.split(","));
		 if(idList.size()==0){
			 return Result.error("数据为空!");
		 }
		 UpdateWrapper<MGlUps> updateWrapper = new UpdateWrapper<>();
		 LambdaUpdateWrapper<MGlUps> lambdaUpdateWrapper = updateWrapper.lambda();
		 idList.forEach(item->lambdaUpdateWrapper.or(qr->qr.eq(MGlUps::getId,item)));
		 lambdaUpdateWrapper.set(MGlUps::getWhetherToMonitor,"1");
		 mGlUpsService.update(updateWrapper);
		 return Result.OK("监控添加成功!");
	 }

	 /**
	  *  取消监控
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "UPS电源-取消监控")
	 @ApiOperation(value="UPS电源-取消监控", notes="UPS电源-取消监控")
	 @RequiresPermissions("UPS电源:取消监控")
	 @RequestMapping(value = "/unmonitor", method = {RequestMethod.POST})
	 @Transactional(rollbackFor = Exception.class)
	 public Result<String> unmonitor(@RequestParam(name="ids",required=true) String ids) {
		 List<String> idList = Arrays.asList(ids.split(","));
		 if(idList.size()==0){
			 return Result.error("数据为空!");
		 }
		 UpdateWrapper<MGlUps> updateWrapper = new UpdateWrapper<>();
		 LambdaUpdateWrapper<MGlUps> lambdaUpdateWrapper = updateWrapper.lambda();
		 idList.forEach(item->lambdaUpdateWrapper.or(qr->qr.eq(MGlUps::getId,item)));
		 lambdaUpdateWrapper.set(MGlUps::getWhetherToMonitor,"0");
		 mGlUpsService.update(updateWrapper);
		 return Result.OK("监控取消成功!");
	 }

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "UPS电源-通过id查询")
	@ApiOperation(value="UPS电源-通过id查询", notes="UPS电源-通过id查询")
	@GetMapping(value = "/queryById")
	@RequiresPermissions("UPS电源:查询")
	public Result<MGlUps> queryById(@RequestParam(name="id",required=true) String id) {
		MGlUps mGlUps = mGlUpsService.getById(id);
		if(mGlUps==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mGlUps);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param mGlUps
    */
	@RequiresPermissions("UPS电源:导出")
    @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
	@ApiOperation(value="UPS电源-导出", notes="UPS电源-导出")
    public ModelAndView exportXls(HttpServletRequest request, MGlUps mGlUps) {
        return super.exportXls(request, mGlUps, MGlUps.class, "UPS电源");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
	@RequiresPermissions("UPS电源:导入")
	@ApiOperation(value="UPS电源-导入", notes="UPS电源-导入")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MGlUps.class);
    }

	 @Override
	 protected  void beforeImportExcel(List<MGlUps> list){
		 list.forEach(item->{
			 if(org.apache.commons.lang.StringUtils.isNotEmpty(item.getMachineRoom())){
				 item.setMachineRoom(item.getMachineRoom().trim());
			 }
			 if(org.apache.commons.lang.StringUtils.isNotEmpty(item.getPersonLiable())){
				 item.setPersonLiable(item.getPersonLiable().trim());
			 }
		 });
	 }

	 @Override
	 protected void afterImportExcel(List<MGlUps> list){
		 UpdateWrapper<MGlUps> updateWrapper = new UpdateWrapper<>();

//		 updateWrapper.clear();
//		 updateWrapper.lambda().isNull(MGlUps::getMachineRoomId);
//		 updateWrapper.setSql("Machine_Room_Id=(select id from m_machine_room where machine_room=name LIMIT 1)");
//		 this.service.update(updateWrapper);

		 updateWrapper.clear();
		 updateWrapper.lambda().isNull(MGlUps::getPersonLiableId);
		 updateWrapper.setSql("person_liable_id=(select id from sys_user where person_liable=realname LIMIT 1)");
		 this.service.update(updateWrapper);
	 }

}
